<?php

abstract class Silk_Module_Type implements Silk_Module_Type_Interface {

    protected $_view_helper_prefix = 'Silk_View_Helper';
    protected $_view_template_suffix = 'html';

    public function getTypeMeta(){
	throw new Silk_Module_Exception('Stub not implemented - getTypeMeta', Silk_Module_Exception::FEATURE_NOT_IMPLEMENTED);
    }
    public function getName(){
	throw new Silk_Module_Exception('Stub not implemented - getName', Silk_Module_Exception::FEATURE_NOT_IMPLEMENTED);
    }
    public function getSlug(){
	throw new Silk_Module_Exception('Stub not implemented - getSlug', Silk_Module_Exception::FEATURE_NOT_IMPLEMENTED);
    }
    public function getDescription(){
	throw new Silk_Module_Exception('Stub not implemented - getDescription', Silk_Module_Exception::FEATURE_NOT_IMPLEMENTED);
    }
    public function getVersion(){
	throw new Silk_Module_Exception('Stub not implemented - getVersion', Silk_Module_Exception::FEATURE_NOT_IMPLEMENTED);
    }

    public function getAuthor(){
	throw new Silk_Module_Exception('Stub not implemented - getAuthor', Silk_Module_Exception::FEATURE_NOT_IMPLEMENTED);
    }

    public function getEmail(){
	throw new Silk_Module_Exception('Stub not implemented - getDescription', Silk_Module_Exception::FEATURE_NOT_IMPLEMENTED);
    }

    public function getWebsite(){
	throw new Silk_Module_Exception('Stub not implemented - getWebsite', Silk_Module_Exception::FEATURE_NOT_IMPLEMENTED);
    }

    public function getAssetPath(){
	return dirname(__FILE__) . '/assets';
    }

    public function getTemplatePath(){
	return dirname(__FILE__) . '/templates';
    }

    public function getHelperPath(){
	return dirname(__FILE__) . '/helpers';
    }

    public function init(){}

    protected function prepare(Silk_Object $pObject=null, array $pVars=array(), $pAction=null, Zend_View_Interface $pView=null){
	if ($pView === null) $pView = $this->getViewEngine();

	$pView->assign('object', $pObject);

	foreach ($pVars as $key => $val)
	    $pView->assign($key, $val);

	return $pView;
    }

    protected function getViewEngine(){
	$view = Silk::getViewEngine();

	$view->addScriptPath($this->getTemplatePath());

	if (file_exists($this->getHelperPath()))
	    $view->addHelperPath($this->getHelperPath(), $this->_view_helper_prefix);

	return $view;
    }

    protected function renderTemplate(Zend_View_Interface $pView, $pTemplateName){

	$view = $pView;

	$template_folder = $this->getTemplatePath();

	if (!file_exists($template_folder) || !is_dir($template_folder))
	    throw new Silk_Exception('Type template folder does not exists - ' . $template_folder );

	$template_name = $pTemplateName . '.' . $this->_view_template_suffix;
	$template_path = "{$template_folder}/{$template_name}";

	if (file_exists($template_path)){

	    return $view->render($template_name);
	} else
	    throw new Silk_Exception('Template does not exist - ' . $template_name);

    }

    protected function renderString(Zend_View_Interface $pView, $pContent){

	$view = $pView;

	return $view->render(stripslashes($pContent));

    }

    public function render(Silk_Object $pObject=null, $pAction=null, array $pVars=array()){

	$view = $this->prepare($pObject, $pVars, $pAction);
	//$view = $this->getViewEngine();

	try {

	    if ($pAction instanceof Silk_Template){
		Silk::debug('Silk_Module_Type.render: Rendering virtual template [' . $pAction->name . ']');
		return $this->renderString($view, $pAction->content);
	    } else if ($pAction){
		Silk::debug('Silk_Module_Type.render: Rendering template [' . $pAction . ']');
		return $this->renderTemplate($view, $pAction);
	    } else
		return $this->renderString($view, $pObject->content);

	} catch (Exception $x){
	    return '<div class="silk-template-error">Failed to render ' . $pAction . '</div>';
	}
    }
}

?>